package com.cyw.search.service.impl;


import cn.easyes.core.biz.PageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.cyw.common.core.domain.R;
import com.cyw.search.entity.note.SearchNoteEntity;
import com.cyw.search.mapper.SearchNoteEntityMapper;
import com.cyw.search.service.SearchNoteEntityService;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Chenyw
 * @Date: 2022/11/02/15:40
 */
@Service
public class SearchNoteEntityServiceImpl implements SearchNoteEntityService {
    @Resource
    private SearchNoteEntityMapper searchNoteEntityMapper;

    @Override
    public R<PageInfo<SearchNoteEntity>> searchAllNote(String searchText, Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<SearchNoteEntity> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.sortByScore(SortOrder.ASC).queryStringQuery(searchText);
        return R.ok(searchNoteEntityMapper.pageQuery(wrapper, pageNum, pageSize));
    }

    @Override
    public SearchNoteEntity findById(String id) {
        /**
         * 增加访问量
         */
        SearchNoteEntity searchNoteEntity = searchNoteEntityMapper.selectById(id);
        searchNoteEntity.setViews(searchNoteEntity.getViews()+1);
        searchNoteEntityMapper.insert(searchNoteEntity);
        return searchNoteEntity;
    }

    @Override
    public List<SearchNoteEntity> findHotNote() {
        LambdaEsQueryWrapper<SearchNoteEntity> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.orderByDesc(SearchNoteEntity::getViews).limit(3);
        return searchNoteEntityMapper.selectList(wrapper);
    }

    @Override
    public SearchNoteEntity writeNote(SearchNoteEntity searchNoteEntity) {
        return searchNoteEntityMapper.insert(searchNoteEntity)>0?searchNoteEntity:null;
    }

    @Override
    public R<PageInfo<SearchNoteEntity>> findAllNote(Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<SearchNoteEntity> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.orderByDesc(SearchNoteEntity::getCreateTime);
        return R.ok(searchNoteEntityMapper.pageQuery(wrapper, pageNum, pageSize));
    }
}
